package pannelli.sub;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

import javax.swing.*;
import javax.swing.border.BevelBorder;

import oggetti.Cliente;
import pannelli.VisualizzaCliente;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SubPannelloCliente extends JPanel{


	private static final long serialVersionUID = -4790828311288817548L;
	private JTextField mail;
	private JTextField codiceFiscale;
	private JTextField iva;
	private JTextField telefono;
	private JTextField residenza;
	private JTextField nome;
	private JTextField cognome;
	private JTextField id;
	private boolean cliccato;
        private static SubPannelloCliente instance;

	private SubPannelloCliente(){
		super();
		initGUI();
		cliccato=false;
		if(VisualizzaCliente.getInstance()!=null)
			mouseListener();
			
	}
        public static SubPannelloCliente getInstance(){
           if(instance==null)
               instance = new SubPannelloCliente();
           return instance;
        }
	private void mouseListener(){
		for(int i=0;i<this.getComponentCount();i++)
			this.getComponent(i).addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent arg0) {
                                    if(!cliccato){
                                        cliccato=true;
                                        VisualizzaCliente.getInstance().setModifica();
                                    }
				}
			});
	}
	
	public void riempiID(){
		String id_;
		try {
			//id_ = new DB().getIDLiberi();
			//if(id_.length()<=0){
				int i = new DB().queryCount("SELECT * FROM clienti");
				id.setText(""+(i+1));
			//}else{
			//	id.setText(id_);
			//}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

	}
	public Cliente getClienteFromFields(){
		String id_,nome_,cognome_,fiscale_,iva_,telefono_,mail_,residenza_;
		id_ = id.getText().length()>=0?id.getText():" ";
		iva_ = iva.getText().length()>=0?iva.getText():" ";
		mail_ = mail.getText().length()>=0?mail.getText():" ";
		fiscale_ = codiceFiscale.getText().length()>=0?codiceFiscale.getText():" ";
		residenza_=residenza.getText().length()>=0?residenza.getText():" ";
		cognome_ = cognome.getText().length()>=0?cognome.getText():" ";
		nome_=nome.getText().length()>=0?nome.getText():" ";
		telefono_ = telefono.getText().length()>=0?telefono.getText():" ";
		Cliente tmp = new Cliente(nome_,cognome_,iva_,fiscale_,residenza_,telefono_,mail_,Integer.parseInt(id_),"","");
		return tmp;
	}
	public String getID(){
		return id.getText();
	}
	public void fillFields(String nome,String cognome,String id,String residenza,String iva, String mail,String fiscale,String telefono){
		this.nome.setText(nome);
		this.cognome.setText(cognome);
		this.residenza.setText(residenza);
		this.telefono.setText(telefono);
		this.iva.setText(iva);
		this.mail.setText(mail);
		this.codiceFiscale.setText(fiscale);
		this.id.setText(id);
	}
	public void fillFields(Cliente c){
		this.nome.setText(c.getNome());
		this.cognome.setText(c.getCognome());
		this.residenza.setText(c.getResidenza());
		this.telefono.setText(c.getTelefono());
		this.iva.setText(c.getIva());
		this.mail.setText(c.getEmail());
		this.codiceFiscale.setText(c.getFiscale());
		this.id.setText(""+c.getId());
	}
	public void deleteAllFields(){
		this.telefono.setText("");
		this.iva.setText("");
		this.nome.setText("");
		this.cognome.setText("");
		this.residenza.setText("");
		this.mail.setText("");
		this.codiceFiscale.setText("");
		riempiID();
	}
	public void disableAllField(){
		this.telefono.setEnabled(false);
		this.iva.setEnabled(false);
		this.nome.setEnabled(false);
		this.cognome.setEnabled(false);
		this.residenza.setEnabled(false);
		this.mail.setEnabled(false);
		this.codiceFiscale.setEnabled(false);
	}
        public void enableAllField(){
		this.telefono.setEnabled(true);
		this.iva.setEnabled(true);
		this.nome.setEnabled(true);
		this.cognome.setEnabled(true);
		this.residenza.setEnabled(true);
		this.mail.setEnabled(true);
		this.codiceFiscale.setEnabled(true);
	}
	private void initGUI(){
		AnchorLayout datiClienteLayout = new AnchorLayout();
		setLayout(datiClienteLayout);
		this.setPreferredSize(new java.awt.Dimension(680, 186));
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		{
			mail = new JTextField();
			this.add(getTelefono(), new AnchorConstraint(389, 606, 642, 24, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.add(getResidenza(), new AnchorConstraint(77, 987, 325, 740, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.add(getCognome(), new AnchorConstraint(77, 733, 325, 421, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.add(getNome(), new AnchorConstraint(77, 403, 325, 159, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.add(mail, new AnchorConstraint(389, 987, 642, 615, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			mail.setPreferredSize(new java.awt.Dimension(253, 47));
			mail.setBorder(BorderFactory.createTitledBorder("E-Mail"));
		}
		{
			codiceFiscale = new JTextField();
			this.add(codiceFiscale, new AnchorConstraint(706, 980, 959, 509, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			codiceFiscale.setPreferredSize(new java.awt.Dimension(320, 47));
			codiceFiscale.setBorder(BorderFactory.createTitledBorder("Codice Fiscale"));
		}
		{
			iva = new JTextField();
			this.add(iva, new AnchorConstraint(706, 480, 959, 21, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			iva.setPreferredSize(new java.awt.Dimension(312, 47));
			iva.setBorder(BorderFactory.createTitledBorder("Partita IVA"));
		}
		{
			id = new JTextField();
			this.add(id, new AnchorConstraint(77, 150, 325, 24, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			id.setPreferredSize(new java.awt.Dimension(86, 46));
			id.setBorder(BorderFactory.createTitledBorder("ID Cliente"));
			id.setEditable(false);
		}
	}
	
	private JTextField getNome() {
		if(nome == null) {
			nome = new JTextField();
			nome.setPreferredSize(new java.awt.Dimension(166, 46));
			nome.setBorder(BorderFactory.createTitledBorder("Nome"));
		}
		return nome;
	}
	
	private JTextField getCognome() {
		if(cognome == null) {
			cognome = new JTextField();
			cognome.setPreferredSize(new java.awt.Dimension(212, 46));
			cognome.setBorder(BorderFactory.createTitledBorder("Cognome"));
		}
		return cognome;
	}
	
	private JTextField getResidenza() {
		if(residenza == null) {
			residenza = new JTextField();
			residenza.setPreferredSize(new java.awt.Dimension(168, 46));
			residenza.setBorder(BorderFactory.createTitledBorder("Telefono"));
		}
		return residenza;
	}
	
	private JTextField getTelefono() {
		if(telefono == null) {
			telefono = new JTextField();
			telefono.setPreferredSize(new java.awt.Dimension(396, 47));
			telefono.setBorder(BorderFactory.createTitledBorder("Residenza"));
		}
		return telefono;
	}

}
